package com.xuef.ibatis.parsing;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;

/**
 * Created by xuefengDong: moveb
 * On 2020/6/20- 20:30
 */
public class XPathParser {
    Document document;

    public XPathParser(InputStream in){
        this.document = createDocument(in);
    }

    Document createDocument(InputStream inputStream){
        //TODO 可以增加文档校验过程
        Document document = null;
        try {
            document = new SAXReader().read(inputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }

    public XNode evalNode(String key) {
        Element element = document.elementByID(key);
        return new XNode(element);
    }

    public XNode evalRoot(String key) {
        Element element = document.getRootElement();
        return new XNode(element);
    }
}
